package tech.aistar.day13.basedao.impl;

import tech.aistar.day13.IOUtil;
import tech.aistar.day13.basedao.IBaseDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 本类用来演示: 顶级接口的实现类
 *
 * @author: success
 * @date: 2021/3/24 3:21 下午
 */
public class BaseDaoImpl<T> implements IBaseDao<T> {

    private IOUtil<T> ioUtil = new IOUtil<>();

    @Override
    public void save(T t,String pathname) {
        //先尝试将原来的数据读取出来
        List<T> oldList = findAll(pathname);
        if(oldList==null || oldList.size()==0){
            List<T> newList = new ArrayList<>();
            newList.add(t);
            ioUtil.writeObject2(newList,pathname);
        }else{
            //原来已经有数据
            oldList.add(t);
            ioUtil.writeObject2(oldList,pathname);
        }
       // System.out.println("oldList:"+oldList);
    }

    @Override
    public List<T> findAll(String pathname) {
        return ioUtil.readObject(pathname);
    }
}
